package com.powernobug.service.impl;

import com.github.pagehelper.PageHelper;
import com.powernobug.bean.model.MarketGoods;
import com.powernobug.bean.model.MarketGoodsExample;
import com.powernobug.mapper.MarketGoodsMapper;
import com.powernobug.annotation.PrintThreadLog;
import com.powernobug.bean.vo.template.GoodsTempForHomeIndex;
import com.powernobug.service.MarketGoodsService;
import com.powernobug.util.TransferModelDataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.Future;

/**
 * @Author: 肖云戈
 * @CreateTime: 2024-08-30
 * @Description:
 */
@Service
public class MarketGoodsServiceImpl implements MarketGoodsService {

    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Override
    @Async("globalThreadPool")
    @PrintThreadLog
    public Future<List<GoodsTempForHomeIndex>> getNewGoodsByLimit(int newGoodsListLimit) {
        MarketGoodsExample example = new MarketGoodsExample();
        example.createCriteria().andDeletedEqualTo(false);
        example.setOrderByClause("add_time desc");
        PageHelper.startPage(1, newGoodsListLimit);

        List<MarketGoods> originData = marketGoodsMapper.selectByExample(example);
        Class<?> resultType = GoodsTempForHomeIndex.class;
        List<GoodsTempForHomeIndex> resultData = (List<GoodsTempForHomeIndex>) TransferModelDataUtil.transferFatherToSon(resultType, originData);
        return new AsyncResult<>(resultData);
    }

    @Override
    @Async("globalThreadPool")
    @PrintThreadLog
    public Future<List<GoodsTempForHomeIndex>> getHotGoodsByLimit(int hotGoodsListLimit) {
        MarketGoodsExample example = new MarketGoodsExample();
        example.createCriteria().andDeletedEqualTo(false);
        example.setOrderByClause("add_time desc");
        PageHelper.startPage(1, hotGoodsListLimit);

        List<MarketGoods> originData = marketGoodsMapper.selectByExample(example);
        Class<?> resultType = GoodsTempForHomeIndex.class;
        List<GoodsTempForHomeIndex> resultData = (List<GoodsTempForHomeIndex>) TransferModelDataUtil.transferFatherToSon(resultType, originData);
        return new AsyncResult<>(resultData);
    }

    @Override
    public List<GoodsTempForHomeIndex> getSecondLevelGoodsListByCategoryIdWithLimit(List<Integer> categoryId, int secondLevelGoodsListLimit) {
        MarketGoodsExample example = new MarketGoodsExample();
        example.createCriteria().andCategoryIdIn(categoryId).andDeletedEqualTo(false);
        example.setOrderByClause("add_time desc");
        PageHelper.startPage(1, secondLevelGoodsListLimit);

        List<MarketGoods> originData = marketGoodsMapper.selectByExample(example);
        Class<?> resultType = GoodsTempForHomeIndex.class;
        return (List<GoodsTempForHomeIndex>) TransferModelDataUtil.transferFatherToSon(resultType, originData);
    }
}
